/*
 * Treasure.cpp
 *
 *  Created on: Sep 2, 2013
 *      Author: jason
 */

#include <string>
#include <stdio.h>
#include "../../include/GameModes/TownMode/TownMode.hpp"
#include "../../include/Objects/Treasure.hpp"
#include "../../include/Common.hpp"

namespace OForce_Object {
Treasure::Treasure(int posX, int posY, int gold, int id, int itemID, int open /*= 0*/,
			int hidden /*= 0*/) 		  : mGold(gold),
										mOpen(open),
										mHidden(hidden),
										mItem(NULL),
										mEmpty(false) {
	mId = id;
	mCollide = true;
	currentLayer = 0;
	std::cout << "Created treasure.\n";
	if (!texture.loadFromFile("data/Sprites/chest_closed.png"))
		std::cout << "Could not load treasure image\n";
	sprite.setTexture(texture);
	objectType = TREASURE_TYPE;
	if (itemID >= 0)
		mItem = new OForce::Item (itemID);
	position = sf::Vector2i(posX, posY);
	sprite.setPosition((float)posX, (float)posY);
	if (!windowImage.loadFromFile("data/primitiveTextBox.png"))
		std::cout << "Could not load text window image\n";
	windowImage.createMaskFromColor(sf::Color(19, 95, 181), 100);
	windowTexture.loadFromImage(windowImage);
	windowSprite.setTexture(windowTexture);
	windowText.setFont(font);
	windowText.setCharacterSize(24);
}

Treasure::~Treasure() {
	if (mItem != NULL)
		delete mItem;
	mItem = NULL;
}

void Treasure::draw() {
	if (!mHidden)
		Window.draw(sprite);
}

void Treasure::open(Character *actor) {
	sf::Vector2f viewCenter = view.getCenter();
	sf::Vector2f viewSize = view.getSize();
	viewSize.x /= 2;
	viewSize.y /= 2;
	windowSprite.setPosition(viewCenter.x-viewSize.x+15.0f,
			viewCenter.y+viewSize.y-220.0f);
	windowSprite.setScale(0.9, 0.5);
	windowText.setPosition(viewCenter.x-viewSize.x+50.0f,
			viewCenter.y+viewSize.y-200.0f);
	if (!mOpen) {
		mOpen = true;
		if (!texture.loadFromFile("data/Sprites/chest_opened.png"))
			std::cout << "Could not load treasure image\n";
		sprite.setTexture(texture);
//		if (mItem != NULL)
//			std::cout << "Contained: " << mItem->getName() << "\n";
//		else
//			std::cout << "Contained: " << mGold << " gold\n";
		// TODO should determine which mode is active
//		if (OForce_Mode::StateMachine::getCurrentInstance()->getState() == OForce_Mode::TownMode)
		OForce_Mode::TownMode::getCurrentInstance()->setState(OForce_Mode::TREASURE);
		// TODO Give character item or gold
		GlobalState::Instance()->addToPartyInventory(mItem->getItemID());
		std::string treasureText = actor->getName();
		treasureText.append(" acquired ");
		if (mItem != NULL)
			treasureText.append(mItem->getName());
		else
			treasureText.append(std::to_string(mGold));
		treasureText.append("!!\n");
		windowText.setString(treasureText);
		dialogueWindow.setString(treasureText);
	} else {
		windowText.setString("Chest was empty!\n");
		dialogueWindow.setString("Chest was empty!\n");
	}
}

void Treasure::drawDialogue() {
//	Window.draw(windowSprite);
//	Window.draw(windowText);
	dialogueWindow.draw();
}

void Treasure::update() {
	// Updates
	dialogueWindow.update();
}

void Treasure::hurry() {
	dialogueWindow.hurry();
}

}
